import BaseScene from "./core/BaseScene";
import EventManager from "./core/EventManager";
import PlayerData from "./data/PlayerData";
import { NetInfo, ProtoList } from "./net/Commond";
import { CommonEvents } from "./net/CommonEvents";
import ParseData from "./net/ParseData";
import { WebSock } from "./net/WebSock";
import { Msg } from "./protobuf/msg";
import LogUtil from "./utils/LogUtil";

const {ccclass, property} = cc._decorator;

@ccclass
export default class chat extends BaseScene {
    @property(cc.EditBox) edx_msg: cc.EditBox = null;
    @property(cc.Node) content: cc.Node = null;
    @property(cc.Prefab) itemChat: cc.Prefab = null;

    private _username:string = '';
    
    onLoad(){
        super.onLoad();
        EventManager.on(CommonEvents.SERVICE_CONNECTED,this.onConnected,this);
        EventManager.on(CommonEvents.SERVICE_MSG,this.onMessage,this);
    }

    onDestroy(){
        super.onDestroy();
        EventManager.off(CommonEvents.SERVICE_CONNECTED,this.onConnected,this);
        EventManager.off(CommonEvents.SERVICE_MSG,this.onMessage,this);
    }

    start(){
        let url = `ws://${NetInfo.ip}:${NetInfo.wsport}`;
        WebSock.ins().connect({url:url});
    }

    onConnected(){
        LogUtil.log(LogUtil.getFileName(),'链接成功');
        this._username = PlayerData.ins().username;
        let data = Msg.ChatMsg.create();
        data.type = 'add';
        data.msg = this._username;
        data.curuser = {
            userid:PlayerData.ins().userid,
            username:PlayerData.ins().username
        }
        let sendmsg = ParseData.ins().reqData('ChatMsg',ProtoList.ChatMsg,data);
        LogUtil.log(LogUtil.getFileName(),'sendmsg>>',sendmsg);
        WebSock.ins().send(sendmsg);
    }

    onMessage(msg){
        LogUtil.log(LogUtil.getFileName(),'msg>>',msg);
        // let bufView = new Uint8Array(msg);
        // LogUtil.log(LogUtil.getFileName(),'bufView>>',bufView);
        let data = ParseData.ins().respData(msg);
        LogUtil.log(LogUtil.getFileName(),'data>>',data);

        let idata;
        if(data.type == 'say'){
            idata = {
                name:data.curuser.username+':',
                msg:data.msg
            }
        }else if(data.type == 'add'){
            idata = {
                name:data.msg+'加入房间',
                msg:""
            }
        }else if(data.type == 'leave'){
            idata = {
                name:data.msg+'离开房间',
                msg:""
            }
        }
       
        let node = cc.instantiate(this.itemChat);
        node.getComponent('itemChat').init(idata);
        this.content.addChild(node);
    }

    onSendClick(){
        let msg = this.edx_msg.string;
        this.edx_msg.string = '';
        LogUtil.log(LogUtil.getFileName(),"发送消息>",msg);
        let data = Msg.ChatMsg.create();
        data.type = 'say';
        data.msg = msg;
        data.curuser = {
            userid:PlayerData.ins().userid,
            username:PlayerData.ins().username
        }
        let sendmsg = ParseData.ins().reqData('ChatMsg',ProtoList.ChatMsg,data);
        LogUtil.log(LogUtil.getFileName(),'sendmsg>>',sendmsg);
        WebSock.ins().send(sendmsg);
    }

    onClearClick(){

    }
}
